import tkinter as tk
from tkinter import messagebox
from ..widgets.animated_button import create_animated_button
from ..styles.colors import BACKGROUNDS, TEXT_COLORS, CLASS_COLORS
from ..styles.fonts import get_font

def show_detail_screen(parent, character, on_back, on_delete_character):
    for widget in parent.winfo_children():
        widget.destroy()
    
    main_container = tk.Frame(parent, bg=BACKGROUNDS["main"])
    main_container.pack(fill="both", expand=True)
    
    header_frame = tk.Frame(main_container, bg=BACKGROUNDS["header"], height=60)
    header_frame.pack(fill="x", pady=(0, 30))
    header_frame.pack_propagate(False)
    
    title_label = tk.Label(
        header_frame,
        text="🔍 角色详情",
        fg=TEXT_COLORS["accent"],
        bg=BACKGROUNDS["header"],
        font=get_font("header", "bold")
    )
    title_label.pack(side="left", padx=20, pady=20)
    
    create_animated_button(
        header_frame,
        "返回",
        on_back,
        style="primary",
        width=8
    ).pack(side="right", padx=20, pady=20)
    
    content_frame = tk.Frame(main_container, bg=BACKGROUNDS["main"])
    content_frame.pack(fill="both", expand=True, padx=50)
    
    card_frame = create_detailed_character_card(content_frame, character, on_delete_character)
    card_frame.pack(expand=True, pady=20)
    
    return main_container

def create_detailed_character_card(parent, character, on_delete):
    card_frame = tk.Frame(
        parent,
        bg=BACKGROUNDS["card"],
        relief="ridge",
        bd=3,
        padx=30,
        pady=30
    )
    
    class_color = CLASS_COLORS.get(character["type"], CLASS_COLORS["其他"])
    
    name_label = tk.Label(
        card_frame,
        text=character["name"],
        fg=TEXT_COLORS["accent"],
        bg=BACKGROUNDS["card"],
        font=("Arial", 20, "bold")
    )
    name_label.pack(pady=(0, 10))
    
    class_frame = tk.Frame(card_frame, bg=BACKGROUNDS["card"])
    class_frame.pack(pady=10)
    
    class_badge = tk.Label(
        class_frame,
        text=f"🏷️ {character['type']}",
        fg=class_color,
        bg=BACKGROUNDS["card"],
        font=("Arial", 14, "bold")
    )
    class_badge.pack()
    
    info_frame = tk.Frame(card_frame, bg=BACKGROUNDS["card"])
    info_frame.pack(pady=20)
    
    level_frame = tk.Frame(info_frame, bg=BACKGROUNDS["card"])
    level_frame.pack(fill="x", pady=5)
    
    tk.Label(
        level_frame,
        text="等级:",
        fg=TEXT_COLORS["secondary"],
        bg=BACKGROUNDS["card"],
        font=("Arial", 12, "bold")
    ).pack(side="left")
    
    level_value = tk.Label(
        level_frame,
        text=f"⭐ {character['level']}",
        fg=TEXT_COLORS["primary"],
        bg=BACKGROUNDS["card"],
        font=("Arial", 12)
    )
    level_value.pack(side="right")
    
    id_frame = tk.Frame(info_frame, bg=BACKGROUNDS["card"])
    id_frame.pack(fill="x", pady=5)
    
    tk.Label(
        id_frame,
        text="角色ID:",
        fg=TEXT_COLORS["secondary"],
        bg=BACKGROUNDS["card"],
        font=("Arial", 12, "bold")
    ).pack(side="left")
    
    id_value = tk.Label(
        id_frame,
        text=character["id"],
        fg=TEXT_COLORS["primary"],
        bg=BACKGROUNDS["card"],
        font=("Arial", 10)
    )
    id_value.pack(side="right")
    
    button_frame = tk.Frame(card_frame, bg=BACKGROUNDS["card"])
    button_frame.pack(pady=(30, 0))
    
    create_animated_button(
        button_frame,
        "删除角色",
        lambda: confirm_delete_dialog(character, on_delete),
        style="danger",
        width=12
    ).pack()
    
    return card_frame

def confirm_delete_dialog(character, on_delete_callback):
    result = messagebox.askyesno(
        "确认删除",
        f"确定要永久删除 {character['name']} 吗？\n这个操作无法撤销！",
        icon="warning"
    )
    
    if result:
        on_delete_callback(character)